package stack.offical;

import java.util.*;

/**
 * @BelongsProject: leetcode
 * @BelongsPackage: stack.offical
 * @Author: Administrator
 * @CreateTime: 2018-12-09 16:30
 * @Description: ${Description}
 */
public class MinStack {
    private Deque<Integer> mainStack; // 主栈，存储所有数据

    private Deque<Integer> minStack; // 将数据压入主栈时，存储最小值

    /** initialize your data structure here. */
    public MinStack() {
        mainStack = new ArrayDeque<>();
        minStack = new ArrayDeque<>();
    }

    public void push(int x) {
        mainStack.push(x);

        Integer min = minStack.peek();
        if (min == null || x <= min) {
            minStack.push(x);
        }
    }



    public int pop() {
        int cur = mainStack.pop();
        if (cur == minStack.peek()) {
            minStack.pop();
        }
        return cur;

    }

    public int top() {
        return mainStack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }


    public String removeDuplicateLetters(String s) {
        char c[]=s.toCharArray();  //转成字符数组
        int i,j;
        int length=0;		//记录有几个相同的字母
        for(i=0;i<c.length-1;i++) {   //如果有重复的字母就把重复的字母置为0
            for(j=i+1;j<c.length;j++) {
                if(c[i]==c[j] && c[i]!=0 && c[j]!=0) {
                    c[j]=0;
                    length++;
                }
            }
        }

        Arrays.sort(c);              //排序

        String end ="";
        for(i=length;i<c.length;i++) {   //输出结果
            end+=c[i];
        }
        return end;


    }

}
